﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trade.DAL;
using Trade.Model;
using Trade.Controls;
using System.Text.RegularExpressions;

namespace Trade
{
    public partial class FrmCustomer : RayForm
    {
        private Customer customer = new Customer();
        //private CustomerInfo customerInfo;
        private Utility.CRUD dataState = Utility.CRUD.None;
        private Common common = new Common();
        private DataTable dtCustomer = null;

        public FrmCustomer()
        {
            InitializeComponent();
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            
            btnOperatorGroup.btnNew.Click += new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        //退出
        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvCustomer.Enabled = true;
            dgvCustomer.Focus();
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            int recordId = 0;

            if (String.IsNullOrEmpty(txtCustomerNo.Text.Trim()))
            {
                MessageBox.Show("客户编号不能为空！", "Prompt ...");
                txtCustomerNo.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtCustomerName.Text.Trim()))
            {
                if (String.IsNullOrEmpty(txtCustomerName.Text.Trim()))
                {
                    MessageBox.Show("供应商名称不能为空！", "Prompt ...");
                    txtCustomerName.Focus();
                    return;
                }
            }

            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.CustomerNo = txtCustomerNo.Text.Trim();
            customerInfo.CustomerName = txtCustomerName.Text.Trim();
            customerInfo.ShortName = txtShortName.Text.Trim();
            customerInfo.Contact = txtContact.Text.Trim();
            customerInfo.ContactTitle = txtContactTitle.Text.Trim();
            customerInfo.Tel = txtTel.Text.Trim();
            customerInfo.Mobile = txtMobile.Text.Trim();
            customerInfo.Fax = txtFax.Text.Trim();
            customerInfo.Address = txtAddress.Text.Trim();
            customerInfo.ZipCode = txtZipCode.Text.Trim();
            customerInfo.BankAccount = txtBankAccount.Text.Trim();
            customerInfo.Bank = txtBank.Text.Trim();
            customerInfo.Credit = String.IsNullOrEmpty(txtCredit.Text.Trim()) ? 0 : Convert.ToDecimal(txtCredit.Text);
            customerInfo.CreditPeriod = String.IsNullOrEmpty(txtCreditPeriod.Text.Trim()) ? 0 : Convert.ToInt32(txtCreditPeriod.Text);
            customerInfo.TaxAccount = txtTaxAccount.Text.Trim();
            customerInfo.Region = cmbRegion.SelectedValue.ToString();
            customerInfo.CustomerType = Convert.ToInt32(cmbCustomerType.SelectedValue.ToString());
            customerInfo.Remark = txtRemark.Text.Trim();
            customerInfo.RecordId = dataState == Utility.CRUD.Insert ? 0 : (int)dgvCustomer.CurrentRow.Cells["RecordId"].Value;

            customer.WriteToDatabase(Utility.CRUDAction(dataState), customerInfo, out recordId);

            if (dataState == Utility.CRUD.Insert)
            {
                /*
                dgvCustomer.Rows.Add();
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["CustomerNo"].Value = customerInfo.CustomerNo;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["CustomerName"].Value = customerInfo.CustomerName;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["ShortName"].Value = customerInfo.ShortName;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Contact"].Value = customerInfo.Contact;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["ContactTitle"].Value = customerInfo.ContactTitle;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Tel"].Value = customerInfo.Tel;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Mobile"].Value = customerInfo.Mobile;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Fax"].Value = customerInfo.Fax;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Address"].Value = customerInfo.Address;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["ZipCode"].Value = customerInfo.ZipCode;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["BankAccount"].Value = customerInfo.BankAccount;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Bank"].Value = customerInfo.Bank;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Credit"].Value = customerInfo.Credit;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["CreditPeriod"].Value = customerInfo.CreditPeriod;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["TaxAccount"].Value = customerInfo.TaxAccount;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Region"].Value = customerInfo.Region;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["CustomerType"].Value = customerInfo.CustomerType;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["TypeName"].Value = cmbCustomerType.Text.Trim();
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["Remark"].Value = customerInfo.Remark;
                dgvCustomer.Rows[dgvCustomer.NewRowIndex].Cells["RecordId"].Value = customerInfo.RecordId;
                */
                
                DataRow dr = customer.GetCustomerByRecordId(recordId);
                dtCustomer.ImportRow(dr);
                dgvCustomer.CurrentRow.Selected = false;
                dgvCustomer.Rows[dgvCustomer.RowCount - 1].Selected = true;
                dgvCustomer.CurrentCell = dgvCustomer.Rows[dgvCustomer.RowCount - 1].Cells["CustomerNo"];     //让当前选中行的Cell得到焦点
                dgvCustomer_SelectionChanged(sender, e);
            }
            else if (dataState == Utility.CRUD.Update)
            { 
                dgvCustomer.CurrentRow.Cells["CustomerNo"].Value = customerInfo.CustomerNo;
                dgvCustomer.CurrentRow.Cells["CustomerName"].Value = customerInfo.CustomerName;
                dgvCustomer.CurrentRow.Cells["ShortName"].Value = customerInfo.ShortName;
                dgvCustomer.CurrentRow.Cells["Contact"].Value = customerInfo.Contact;
                dgvCustomer.CurrentRow.Cells["ContactTitle"].Value = customerInfo.ContactTitle;
                dgvCustomer.CurrentRow.Cells["Tel"].Value = customerInfo.Tel;
                dgvCustomer.CurrentRow.Cells["Mobile"].Value = customerInfo.Mobile;
                dgvCustomer.CurrentRow.Cells["Fax"].Value = customerInfo.Fax;
                dgvCustomer.CurrentRow.Cells["Address"].Value = customerInfo.Address;
                dgvCustomer.CurrentRow.Cells["ZipCode"].Value = customerInfo.ZipCode;
                dgvCustomer.CurrentRow.Cells["BankAccount"].Value = customerInfo.BankAccount;
                dgvCustomer.CurrentRow.Cells["Bank"].Value = customerInfo.Bank;
                dgvCustomer.CurrentRow.Cells["Credit"].Value = customerInfo.Credit;
                dgvCustomer.CurrentRow.Cells["CreditPeriod"].Value = customerInfo.CreditPeriod;
                dgvCustomer.CurrentRow.Cells["TaxAccount"].Value = customerInfo.TaxAccount;
                dgvCustomer.CurrentRow.Cells["Region"].Value = customerInfo.Region;
                dgvCustomer.CurrentRow.Cells["CustomerType"].Value = customerInfo.CustomerType;
                dgvCustomer.CurrentRow.Cells["TypeName"].Value = cmbCustomerType.Text.Trim();
                dgvCustomer.CurrentRow.Cells["Remark"].Value = customerInfo.Remark;
                dgvCustomer.CurrentRow.Cells["RecordId"].Value = customerInfo.RecordId;
            }
            else if (dataState == Utility.CRUD.Delete)
            {
                dgvCustomer.Rows.Remove(dgvCustomer.Rows[dgvCustomer.CurrentRow.Index]);
            }

            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvCustomer.Enabled = true;
            dgvCustomer.Focus();
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvCustomer.RowCount != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                    dgvCustomer.Enabled = false;
                }
            }
        }

        //编辑
        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.RecordId = (int)dgvCustomer.CurrentRow.Cells["RecordId"].Value;

            common.SetControlsState(this, true);
            dgvCustomer.Enabled = false;
            txtCustomerNo.Enabled = false;
            txtCustomerName.Focus();
        }

        //新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, true);
            dgvCustomer.Enabled = false;

            txtCustomerNo.Text = "";
            txtCustomerName.Text = "";
            txtShortName.Text = "";
            txtContact.Text = "";
            txtContactTitle.Text = "";
            txtTel.Text = "";
            txtMobile.Text = "";
            txtFax.Text = "";
            txtAddress.Text = "";
            txtZipCode.Text = "";
            txtBankAccount.Text = "";
            txtBank.Text = "";
            txtCredit.Text = "";
            txtCreditPeriod.Text = "";
            txtTaxAccount.Text = "";
            cmbRegion.SelectedIndex = 0;
            cmbCustomerType.SelectedIndex = 0;
            txtRemark.Text = "";

            txtCustomerNo.Focus();
        }

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            dtCustomer = customer.GetCustomerList();

            dgvCustomer.DataSource = dtCustomer;
            dgvCustomer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            SetCustomerDataGridView();
            
            cmbRegion.Tag = "R000";
            cmbCustomerType.Tag = "C000";

            common.ComboBoxBindingDataSource(cmbRegion, "SubjectName", "SubjectName");
            common.ComboBoxBindingDataSource(cmbCustomerType, "SubjectName", "SubjectId");

            if (dgvCustomer.CurrentRow != null)
            {
                dgvCustomer.CurrentRow.Selected = true;
            }

            dgvCustomer.Focus();
        }

        private void SetCustomerDataGridView()
        {
            dgvCustomer.AllowUserToAddRows = false;
            dgvCustomer.AllowUserToDeleteRows = false;
            dgvCustomer.ReadOnly = true;

            dgvCustomer.Columns[0].Width = 80;
            dgvCustomer.Columns[0].Name = dtCustomer.Columns["CustomerNo"].ColumnName.ToString();
            dgvCustomer.Columns[0].HeaderText = "客户编号";
            dgvCustomer.Columns[1].Width = 200;
            dgvCustomer.Columns[1].Name = dtCustomer.Columns["CustomerName"].ColumnName.ToString();
            dgvCustomer.Columns[1].HeaderText = "客户名称";
            dgvCustomer.Columns[2].Width = 100;
            dgvCustomer.Columns[2].Name = dtCustomer.Columns["ShortName"].ColumnName.ToString();
            dgvCustomer.Columns[2].HeaderText = "简称";
            dgvCustomer.Columns[3].Width = 80;
            dgvCustomer.Columns[3].Name = dtCustomer.Columns["Contact"].ColumnName.ToString();
            dgvCustomer.Columns[3].HeaderText = "联系人";
            dgvCustomer.Columns[4].Width = 80;
            dgvCustomer.Columns[4].Name = dtCustomer.Columns["ContactTitle"].ColumnName.ToString();
            dgvCustomer.Columns[4].HeaderText = "称谓";
            dgvCustomer.Columns[4].Frozen = true;
            dgvCustomer.Columns[5].Width = 80;
            dgvCustomer.Columns[5].Name = dtCustomer.Columns["Tel"].ColumnName.ToString();
            dgvCustomer.Columns[5].HeaderText = "电话";
            dgvCustomer.Columns[6].Width = 80;
            dgvCustomer.Columns[6].Name = dtCustomer.Columns["Mobile"].ColumnName.ToString();
            dgvCustomer.Columns[6].HeaderText = "移动电话";
            dgvCustomer.Columns[7].Width = 80;
            dgvCustomer.Columns[7].Name = dtCustomer.Columns["Fax"].ColumnName.ToString();
            dgvCustomer.Columns[7].HeaderText = "传真";
            dgvCustomer.Columns[8].Width = 150;
            dgvCustomer.Columns[8].Name = dtCustomer.Columns["Address"].ColumnName.ToString();
            dgvCustomer.Columns[8].HeaderText = "地址";
            dgvCustomer.Columns[9].Width = 80;
            dgvCustomer.Columns[9].Name = dtCustomer.Columns["ZipCode"].ColumnName.ToString();
            dgvCustomer.Columns[9].HeaderText = "邮编";
            dgvCustomer.Columns[10].Width = 150;
            dgvCustomer.Columns[10].Name = dtCustomer.Columns["BankAccount"].ColumnName.ToString();
            dgvCustomer.Columns[10].HeaderText = "银行账号";
            dgvCustomer.Columns[10].Visible = false;
            dgvCustomer.Columns[11].Width = 100;
            dgvCustomer.Columns[11].Name = dtCustomer.Columns["Bank"].ColumnName.ToString();
            dgvCustomer.Columns[11].HeaderText = "开户行";
            dgvCustomer.Columns[11].Visible = false;
            dgvCustomer.Columns[12].Width = 80;
            dgvCustomer.Columns[12].Name = dtCustomer.Columns["Credit"].ColumnName.ToString();
            dgvCustomer.Columns[12].HeaderText = "受信额度";
            dgvCustomer.Columns[12].Visible = false;
            dgvCustomer.Columns[13].Width = 80;
            dgvCustomer.Columns[13].Name = dtCustomer.Columns["CreditPeriod"].ColumnName.ToString();
            dgvCustomer.Columns[13].HeaderText = "收款期限";
            dgvCustomer.Columns[13].Visible = false;
            dgvCustomer.Columns[14].Width = 150;
            dgvCustomer.Columns[14].Name = dtCustomer.Columns["TaxAccount"].ColumnName.ToString();
            dgvCustomer.Columns[14].HeaderText = "税号";
            dgvCustomer.Columns[14].Visible = false;
            dgvCustomer.Columns[15].Width = 80;
            dgvCustomer.Columns[15].Name = dtCustomer.Columns["Region"].ColumnName.ToString();
            dgvCustomer.Columns[15].HeaderText = "地区";
            dgvCustomer.Columns[15].Visible = false;
            dgvCustomer.Columns[16].Width = 100;
            dgvCustomer.Columns[16].Name = dtCustomer.Columns["CustomerType"].ColumnName.ToString();
            dgvCustomer.Columns[16].HeaderText = "类型";
            dgvCustomer.Columns[16].Visible = false;
            dgvCustomer.Columns[17].Width = 100;
            dgvCustomer.Columns[17].Name = dtCustomer.Columns["TypeName"].ColumnName.ToString();
            dgvCustomer.Columns[17].HeaderText = "客户类型";
            dgvCustomer.Columns[17].Visible = false;
            dgvCustomer.Columns[18].Width = 200;
            dgvCustomer.Columns[18].Name = dtCustomer.Columns["Remark"].ColumnName.ToString();
            dgvCustomer.Columns[18].HeaderText = "备注";
            dgvCustomer.Columns[18].Visible = false;
            dgvCustomer.Columns[19].Width = 80;
            dgvCustomer.Columns[19].Name = dtCustomer.Columns["RecordId"].ColumnName.ToString();
            dgvCustomer.Columns[19].HeaderText = "记录ID";
            dgvCustomer.Columns[19].Visible = false;

            dgvCustomer.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void txtCustomerNo_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrEmpty(txtCustomerNo.Text.Trim()) == false)
            {
                /*
                if (Regex.IsMatch(txtCustomerNo.Text.Trim(), "C[0-9]{7}") == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("客户编号格式必须是: C0000000", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtCustomerNo.Select(0, txtCustomerNo.Text.Trim().Length);
                    return;
                }*/

                if (customer.IsUniqueCustomerNo(txtCustomerNo.Text.Trim(), dataState == Utility.CRUD.Insert ? 0 : (int)dgvCustomer.CurrentRow.Cells["RecordId"].Value) == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("当前客户编号已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtCustomerNo.Select(0, txtCustomerNo.Text.Trim().Length);
                }
            }
        }

        private void txtCustomerName_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrEmpty(txtCustomerName.Text.Trim()) == false)
            {
                if (customer.IsUniqueCustomerName(txtCustomerName.Text.Trim(), dataState == Utility.CRUD.Insert ? 0 : (int)dgvCustomer.CurrentRow.Cells["RecordId"].Value) == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("当前客户名称已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtCustomerName.Select(0, txtCustomerName.Text.Trim().Length);
                }
            }
        }

        private void txtShortName_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrEmpty(txtShortName.Text.Trim()) == false)
            {
                if (customer.IsUniqueShortName(txtShortName.Text.Trim(), dataState == Utility.CRUD.Insert ? 0 : (int)dgvCustomer.CurrentRow.Cells["RecordId"].Value) == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("当前客户简称已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtShortName.Select(0, txtShortName.Text.Trim().Length);
                }
            }
        }

        private void dgvCustomer_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCustomer.RowCount != 0)
            {
                txtCustomerNo.Text = dgvCustomer.CurrentRow.Cells["CustomerNo"].Value.ToString();
                txtCustomerName.Text = dgvCustomer.CurrentRow.Cells["CustomerName"].Value.ToString();
                txtShortName.Text = dgvCustomer.CurrentRow.Cells["ShortName"].Value.ToString();
                txtContact.Text = dgvCustomer.CurrentRow.Cells["Contact"].Value.ToString();
                txtContactTitle.Text = dgvCustomer.CurrentRow.Cells["ContactTitle"].Value.ToString();
                txtTel.Text = dgvCustomer.CurrentRow.Cells["Tel"].Value.ToString();
                txtMobile.Text = dgvCustomer.CurrentRow.Cells["Mobile"].Value.ToString();
                txtFax.Text = dgvCustomer.CurrentRow.Cells["Fax"].Value.ToString();
                txtAddress.Text = dgvCustomer.CurrentRow.Cells["Address"].Value.ToString();
                txtZipCode.Text = dgvCustomer.CurrentRow.Cells["ZipCode"].Value.ToString();
                txtBankAccount.Text = dgvCustomer.CurrentRow.Cells["BankAccount"].Value.ToString();
                txtBank.Text = dgvCustomer.CurrentRow.Cells["Bank"].Value.ToString();
                txtCredit.Text = dgvCustomer.CurrentRow.Cells["Credit"].Value.ToString();
                txtCreditPeriod.Text = dgvCustomer.CurrentRow.Cells["CreditPeriod"].Value.ToString();
                txtTaxAccount.Text = dgvCustomer.CurrentRow.Cells["TaxAccount"].Value.ToString();
                cmbRegion.SelectedValue = dgvCustomer.CurrentRow.Cells["Region"].Value.ToString();
                cmbCustomerType.SelectedValue = dgvCustomer.CurrentRow.Cells["CustomerType"].Value.ToString();
                txtRemark.Text = dgvCustomer.CurrentRow.Cells["Remark"].Value.ToString();
            }
        }

        private void SearchConditionSetting()
        { 
            //tscmbFields.
        }

        private void FrmCustomer_Shown(object sender, EventArgs e)
        {
            dgvCustomer.Select();
        }
    }
}
